<?php

class Controller_Fight extends Controller_Abstract
{
    public function preDispatch()
    {
        if ($this->_user['status'] != Model_User::STATUS_SAILING) {
            throw new Core_Exception_Logic('你尚未出海，无法参与战斗');
        }
    }

    /**
     * 选择对手
     */
    public function opponentAction()
    {
        $opponentList = $this->_user->getFightOpponent();
        $this->assign('opponentList', $opponentList);
    }

    /**
     * 进入战斗
     */
    public function fightAction()
    {
        $enemyUid = xintval($this->getx('enemy_uid'));
        if ($enemyUid < 1 || $enemyUid == $this->_user['uid']) {
            exit('Invalid EnemyUid');
        }

        // 敌方用户信息
        $enemy = new Model_User($enemyUid);

        // 初始化一场战斗
        $fight = new Model_Fight($this->_user, $enemy);

        // 双方战前布阵信息
        $selfShips  = $fight->getSelfShips();
        $enemyShips = $fight->getEnemyShips();

        // 正式开打!
        $fightResult = $fight->process();

        // 战后相关事宜处理（加减属性等）
        $fightResultMsg = $fight->after();

        // 战斗记录
        $fireLogs = $fight->getFireLogs();

        // 传出模板变量
        $this->assign(array(
            'selfShips'      => $selfShips,
            'enemyShips'     => $enemyShips,
            'fightResult'    => $fightResult,
            'fightResultMsg' => $fightResultMsg,
            'fireLogs'       => $fireLogs,
        ));
    }
}